其他
iOS inlinehook绕过反调试
静态内存匹配特征patch
实现一个简单的inlinehook动态hook绕过
"mov x0,#31\n"
"mov x1,#0\n"
"mov x2,#0\n"
"mov x3,#0\n"
"mov x16,#26\n"
"svc #128\n"
);
int offset = 0;
struct mach_header_64* header = (struct mach_header_64*)_dyld_get_image_header(0);
if(header->magic != MH_MAGIC_64) {
return ;
}
offset = sizeof(struct mach_header_64);
int ncmds = header->ncmds;
while(ncmds--) {
/* go through all load command to find __TEXT segment*/
struct load_command * lcp = (struct load_command *)((uint8_t*)header + offset);
offset += lcp->cmdsize;
if(lcp->cmd == LC_SEGMENT_64) {
struct segment_command_64 * curSegment = (struct segment_command_64 *)lcp;
struct section_64* curSection = (struct section_64*)((uint8_t*)curSegment + sizeof(struct segment_command_64));
// check current section of segment is __TEXT?
if(!strcmp(curSection->segname, "__TEXT") && !strcmp(curSection->sectname, "__text")){
uint64_t memAddr = curSection->addr;
textSegRange->start = memAddr + _dyld_get_image_vmaddr_slide(0);
textSegRange->end = textSegRange->start + curSection->size;
break;
}
}
}
return ;
}
uint8_t * p = (uint8_t*)target_addr;
for(int i = 0; i < size ;i++ ){
/*
mov x16, #0x1a -> 0xd2800350
svc #0x80 -> 0xd4001001
*/
if (*((uint32_t*)p) == 0xd2800350 && *((uint32_t*)p+1) == 0xd4001001) {
return p;
}
p++;
}
return NULL;
}
svc #0x80 -> 0xd4001001
patchCode(ptrace_svc_p+4, patch_ins_data , 4);
struct segmentRange textSegRange;
getTextSegmentAddr(&textSegRange);
void* ptrace_svc_p = lookup_ptrace_svc((void*)textSegRange.start, textSegRange.end-textSegRange.start);
if (!ptrace_svc_p) {
ADDLOG(@"[-] not found ptrace svc");
return;
}
ADDXLOG(@"[+] found ptrace svc # address=%p", ptrace_svc_p);
char* ptrace_bytes = hex_dump((void*)ptrace_svc_p, 8);
ADDXLOG(@"[+] read ptrace svc ins address:%p size:0x%x inst_bytes:%s", ptrace_svc_p, 8, ptrace_bytes);
free(ptrace_bytes);
ADDLOG(@"[*] start to ptach ptrace svc to ret");
uint8_t patch_ins_data[4] = {0x1f, 0x20, 0x03, 0xd5};
patchCode(ptrace_svc_p+4, patch_ins_data , 4);
ADDLOG(@"[*] ptach ptrace svc to nop done, read new value");
ptrace_bytes = hex_dump((void*)ptrace_svc_p, 8);
ADDXLOG(@"[+] read ptrace svc ins address:%p size:0x%x inst_bytes:%s", ptrace_svc_p, 8, ptrace_bytes);
free(ptrace_bytes);
}
getTextSegmentAddr(&textSegRange);
void* svc_p = lookup_svc_ins((void*)textSegRange.start, textSegRange.end-textSegRange.start);
if (!svc_p) {
ADDLOG(@"[-] not found svc");
return;
}
ADDXLOG(@"[+] found svc # address=%p", svc_p);
char* svc_bytes = hex_dump((void*)svc_p, 4);
ADDXLOG(@"[+] read ptrace svc ins address:%p size:0x%x inst_bytes:%s", svc_p, 4, svc_bytes);
free(svc_bytes);
xia0Hook(svc_p);
int len = (int)sysconf(_SC_PAGESIZE);
// 1. get target address page and patch offset
unsigned long page_start = (unsigned long) (target_addr) & ~PAGE_MASK;
unsigned long patch_offset = (unsigned long)target_addr - page_start;
printf("[*] Target address:%p Page start:%p Patch offset:%p", target_addr, (void*)page_start, (void*)patch_offset);
// 2. map new page for patch
void *new = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
if (!new ){
printf("[-] mmap failed!");
return false;
}
// 3.copy target 4 ins to new page
int copy_size = 4*4;
void* copy_from_addr = target_addr - copy_size;
memcpy((void *)(new), copy_from_addr, copy_size);
/*
cmp x16, #0x1a
b.ne loc_not_ptrace_svc_jmp
ldr x17, #0x8
br x17
orig_svc_next_addr_1
orig_svc_next_addr_2
ldr x17, #0x8
br x17
orig_svc_addr_1
orig_svc_addr_2
*/
uint64_t orig_svc_addr = (uint64_t)target_addr;
uint64_t orig_svc_next_addr = (uint64_t)(target_addr+1*4);
uint8_t check_jmp_data[] = {0x1f, 0x6a, 0x00, 0xf1, 0x51, 0x00, 0x00, 0x58, 0x20, 0x02, 0x1f, 0xd6, orig_svc_next_addr&0xff, (orig_svc_next_addr>>8*1)&0xff, (orig_svc_next_addr>>8*2)&0xff, (orig_svc_next_addr>>8*3)&0xff, (orig_svc_next_addr>>8*4)&0xff, (orig_svc_next_addr>>8*5)&0xff, (orig_svc_next_addr>>8*6)&0xff, (orig_svc_next_addr>>8*7)&0xff, 0x51, 0x00, 0x00, 0x58, 0x20, 0x02, 0x1f, 0xd6, orig_svc_addr&0xff, (orig_svc_addr>>8*1)&0xff, (orig_svc_addr>>8*2)&0xff, (orig_svc_addr>>8*3)&0xff, (orig_svc_addr>>8*4)&0xff, (orig_svc_addr>>8*5)&0xff, (orig_svc_addr>>8*6)&0xff, (orig_svc_addr>>8*7)&0xff};
int check_jmp_data_size = 10*4;
memcpy((void *)(new+4*4), check_jmp_data, check_jmp_data_size);
// 4.patch target address to jmp hook code
void* patch_addr = copy_from_addr;
uint64_t new_p = (uint64_t)new;
/*
ldr x16, #0x8
br x16
hook_code_addr_1
hook_code_addr_2
*/
uint8_t patch_data[] = {0x50, 0x00, 0x00, 0x58, 0x00, 0x02, 0x1f, 0xd6,new_p&0xff, (new_p>>8*1)&0xff, (new_p>>8*2)&0xff, (new_p>>8*3)&0xff, (new_p>>8*4)&0xff, (new_p>>8*5)&0xff, (new_p>>8*6)&0xff, (new_p>>8*7)&0xff};
int patch_data_size = 4*4;
patchCode(patch_addr, patch_data, patch_data_size);
// 5. set new page to r-x
mprotect(new, len, PROT_READ | PROT_EXEC);
return true;
}
b.ne loc_not_ptrace_svc_jmp
ldr x17, #0x8
br x17
orig_svc_next_addr_1
orig_svc_next_addr_2
ldr x17, #0x8
br x17
orig_svc_addr_1
orig_svc_addr_2
br x16
hook_code_addr_1
hook_code_addr_2
相对寻址指令的修复问题,以及hook代码的稳定兼容扩展问题。
抽离相关代码,集成到xia0LLDB之中,真正实现调试器中一键绕过反调试。
看雪ID:xia0
https://bbs.pediy.com/user-766212.htm
推荐文章++++
* Linux Kernel Exploit 内核漏洞学习(4)-RW Any Memory